package com.zhuiyun.project.config.MybatisPlus;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.zhuiyun.project.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

/*
* @Author xy
* @Description todo 自定义多重规则租户校验
* @Date 2023/12/1 15:55
* @Return
* @param
*/
@Slf4j
public class TenantMultiHandler implements TenantLineHandler {
    private final TenantProperties tenantProperties;

    public TenantMultiHandler(TenantProperties tenantProperties) {
        this.tenantProperties = tenantProperties;
    }

    /*
    * @Author xy
    * @Description todo 获取租户ID
    * @Date 2023/12/1 15:57
    * @Return java.lang.String
    * @param
    */
    @Override
    public String getTenantIdColumn() {
        return tenantProperties.getColumn();
    }

    /*
    * @Author xy
    * @Description todo 先设置为1.后续根据threadLocal数据进行租户ID判断
    * @Date 2023/12/1 15:58
    * @Return net.sf.jsqlparser.expression.Expression
    * @param
    */
    @Override
    public Expression getTenantId() {
        //TODO 从redis获取当前租户唯一标识，取出租户ID
        String venueUniqueIdentifier = (String) RedisUtil.get("venueUniqueIdentifier");
        return new StringValue(venueUniqueIdentifier);
    }

    /*
    * @Author xy
    * @Description todo 过滤表
    * @Date 2023/12/1 15:59
    * @Return
    * @param
    */
    @Override
    public boolean ignoreTable(String tableName) {
        //自定义注解判断优先级靠前 flag的值为 `true` 表示进行多租户过滤
        return !"fitness_action".equalsIgnoreCase(tableName);
    }

}
